package com.easybbs.controller.api;

import com.easybbs.annotation.Globallnterceptor;
import com.easybbs.annotation.VerifyParam;
import com.easybbs.controller.BaseController;
import com.easybbs.entity.config.WebConfig;
import com.easybbs.entity.enums.ResponseCodeEnum;
import com.easybbs.entity.vo.ResponseVO;
import com.easybbs.exception.BusinessException;
import com.easybbs.service.SysSettingService;
import com.easybbs.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("innerApiController")
@RequestMapping("/innerApi")
public class InnerApiController extends BaseController {
    @Autowired
    private WebConfig webConfig;
    @Autowired
    private SysSettingService sysSettingService;

    /**
     * 内部接口调用
     *
     * @param appKey
     * @param timestamp
     * @param sign
     * @return
     */
    @RequestMapping("/refresSysSetting")
    @Globallnterceptor(checkParams = true)
    public ResponseVO refresSysSetting(@VerifyParam(required = true) String appKey,
                                       @VerifyParam(required = true) Long timestamp,
                                       @VerifyParam(required = true) String sign) {
        if (!webConfig.getInnerApiAppkey().equals(appKey)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        if (System.currentTimeMillis() - timestamp > 1000 * 10) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        String mySing = StringTools.encodeMd5(appKey + timestamp + webConfig.getInnerApiAppSecret());
        if (!mySing.equals(sign)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        return getSuccessResponseVO(sysSettingService.refreshCache());
    }
}
